package com.example.a086.httpdemo.http.base;

import android.content.Context;

import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by 086 on 2018/5/8.
 */

public class HttpUtils {

    private final int GET = 0x11;
    private final int POST = 0x12;

    private Context mContext;
    private String mUrl;
    private Map<String, Object> mParams;
    private Map<String, File> mFiles;
    private String mBody;
    private int mType=POST;
    private static IHttpEngine sIHttpEngine;


    public static void init(IHttpEngine engine) {
        sIHttpEngine = engine;
    }

    public static HttpUtils with(Context context) {
        return new HttpUtils(context);
    }

    public HttpUtils(Context context) {
        mContext = context;
        mParams = new HashMap<>();
        mFiles = new HashMap<>();
    }

    public HttpUtils addFile(String key, File file) {
        mFiles.put(key, file);
        return this;
    }

    public HttpUtils addFiles(Map<String, File> map) {
        mFiles.putAll(map);
        return this;
    }

    public HttpUtils get() {
        mType = GET;
        return this;
    }

    public HttpUtils post() {
        mType = POST;
        return this;
    }

    public HttpUtils url(String url) {
        mUrl = url;
        return this;
    }

    public HttpUtils body(String body) {
        mBody = body;
        return this;
    }

    public HttpUtils addParam(String key, Object value) {
        mParams.put(key, value);
        return this;
    }

    public HttpUtils addParams(Map<String, Object> map) {
        mParams.putAll(map);
        return this;
    }

    public HttpUtils exchangeEngine(IHttpEngine engine) {
        sIHttpEngine = engine;
        return this;
    }

    public void excute(EngineCallback callback) {
        if (callback == null) {
            callback = EngineCallback.DEFAULT_ENGINE_CALLBACK;
        }
        if (mType == GET) {
            sIHttpEngine.get(mContext, mUrl, mParams, callback);
        }
        if (mType == POST) {
            if (mFiles.size()>0){
                sIHttpEngine.uploadFile(mUrl,mParams,mFiles,callback);
            }else {
                sIHttpEngine.post(mContext, mUrl, mBody, callback);
            }
        }
    }

    public static String jointParams(String url, Map<String, Object> params) {
        if (params == null || params.size() <= 0) {
            return url;
        }

        StringBuffer stringBuffer = new StringBuffer(url);
        if (!url.contains("?")) {
            stringBuffer.append("?");
        } else {
            if (!url.endsWith("?")) {
                stringBuffer.append("&");
            }
        }

        for (Map.Entry<String, Object> entry : params.entrySet()) {
            stringBuffer.append(entry.getKey() + "=" + entry.getValue() + "&");
        }

        stringBuffer.deleteCharAt(stringBuffer.length() - 1);

        return stringBuffer.toString();
    }

    public static Class<?> analysisClazzInfo(Object object) {
        Type genType = object.getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        return (Class<?>) params[0];
    }
}
